(function($) {
	
	$(document).bind("contextmenu", function(e) {   
    	return false;   
    });
	
	$.format = function() {
		var s = arguments[0];
		for (var i = 0; i < arguments.length - 1; i++) {
			var reg = new RegExp("\\{" + i + "\\}", "gm");
			s = s.replace(reg, arguments[i + 1]);
		}
		return s;
	};
	
	$.bytes = function() {
		var str = arguments[0];
		return str.replace(/[^\x00-\xff]/g, "**").length;
	};
	
	$.alert = function(title, message, callback) {
		$('#dialog-alert').remove();
		var div = $(document.createElement('div'));
		div.attr('id', 'dialog-alert').attr('title', title);
		var p = $(document.createElement('p'));
		$(p).html(message);
		div.append(p);
		$("body").append(div);
		
		$('#dialog-alert').dialog({
			modal: true,
			buttons: {
				OK: function() {
					if (callback && typeof(callback) == "function") {
						callback.call();
					}
					$(this).dialog("close");
				}
			},
			close: function(event, ui) {
				$('#dialog-alert').remove();
			}
	    });
	};
	
	$.confirm = function(title, message, callback) {
		$('#dialog-confirm').remove();
		var div = $(document.createElement('div'));
		div.attr('id', 'dialog-confirm').attr('title', title);
		var p = $(document.createElement('p'));
		$(p).html(message);
		div.append(p);
		$("body").append(div);
		
		$('#dialog-confirm').dialog({
			modal: true,
			buttons: {
				OK: function() {
					callback.onOk();
					$(this).dialog("close");
				},
				Cancel: function() {
					callback.onCancel();
					$(this).dialog("close");
				}
			},
			close: function(event, ui) {
				$('#dialog-confirm').remove();
			}
	    });
	};
	
	$.loadImage = function(attachmentId, urlPrefix, callback) {
		var attachmentAPI = new AttachmentAPI({
			urlPrefix : urlPrefix
		});
		
		var paramData = {};
		paramData.attachment_id = attachmentId;
		attachmentAPI.loadImage({
			onSuccess : function(imageData) {
				if (imageData.image) {
					callback.onSuccess(imageData.image);
				} else {
					callback.onNoImage();
				}
			},
			onAPIError : function(status, errors) {
				callback.onError(errors);
			},
			onServerError : function(jqXHR, textStatus, errorThrown) {
				callback.onError(errorThrown);
			}
		},
		{
			param: JSON.stringify(paramData)
		});
	}
    
	$.codeNameCombo = function($option) {
        var codeNameAPI = new CodeNameAPI({
            urlPrefix : $option.urlPrefix
        });
        
        var oldValue;

        var paramData = {};
        paramData.type_id = $option.typeId;
        codeNameAPI.search({
            onSuccess : function(codeNameList) {
                if ($option.blank) {
                    var blank = {};
                    if ($option.blankValue) {
                        blank.code = $option.blankValue;
                    } else {
                        blank.code = "";
                    }
                    
                    if ($option.blankText) {
                        blank.name = $option.blankText;
                    } else {
                        blank.name = "";
                    }
                    codeNameList.unshift(blank);
                }
                
                var target = $($option.target);
                if (target && codeNameList.length > 0) {
                    $.each(codeNameList, function(index, row) {
                        var excludeFlag = false;
                        if ($option.exclude && $.isArray($option.exclude) && $option.exclude.length > 0) {
                            $.each($option.exclude, function(index2, row2) {
                                if (row2 == row.code) {
                                    excludeFlag = true;
                                    return false;
                                }
                            });
                        }
                        if (!excludeFlag) {
                            target.append('<option value="' + row.code + '" ' + ($option.value && $option.value == row.code ? 'selected="selected"' : '') + '>' + row.name + '</option>');
                        }
                    });
                    if ($option.onChange && typeof($option.onChange) == "function") {
                        target.focus(function() {
                            oldValue = this.value;
                        });
                        target.change(function() {
                            $option.onChange(this.value, oldValue);
                            oldValue = this.value;
                        });
                    }
                }
            },
            onAPIError : function(status, errors) {
            },
            onServerError : function(jqXHR, textStatus, errorThrown) {
            }
        },
        {
            param: JSON.stringify(paramData)
        });
    };
	
})(jQuery);